package com.xoms.admin.controller.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;
import com.xoms.admin.controller.res.*;
import com.xoms.admin.controller.res.bean.*;
import com.xoms.admin.controller.springmvc.req.*;

/**
 * 商品管理相关接口
 **/
@Controller
@RequestMapping(value = "/product", produces = "application/json")
public class SProductController {
	@Autowired
	com.xoms.admin.controller.ProductController productController;

	/**
	 * 获取所有商品分类列表
	 **/
	@RequestMapping("/v1/getAllCategories")
	@ResponseBody
	public ObjectResult<RProCategoryJSONBean> getAllCategories(HttpServletRequest request, HttpServletResponse response) {

		return productController.getAllCategories(request, response);
	}

	/**
	 * 获取商品分类树形列表
	 **/
	@RequestMapping("/v1/getCategories")
	@ResponseBody
	public ObjectResult<RProCategoryJSONBean> getCategories(@Validated GetCategorieParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProCategoryJSONBean> res = new ObjectResult<RProCategoryJSONBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getCategories(param, request, response);
	}

	/**
	 * 获取商品一级分类列表
	 **/
	@RequestMapping("/v1/getFirstCategories")
	@ResponseBody
	public ObjectResult<RProCategoryJSONBean> getFirstCategories(HttpServletRequest request, HttpServletResponse response) {

		return productController.getFirstCategories(request, response);
	}

	/**
	 * 新建、编辑商品分类
	 **/
	@RequestMapping("/v1/editCategory")
	@ResponseBody
	public ObjectResult<RProCategoryBean> editCategory(@Validated EditProCategoryParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProCategoryBean> res = new ObjectResult<RProCategoryBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editCategory(param, request, response);
	}

	/**
	 * 删除分类信息
	 **/
	@RequestMapping("/v1/delCategory")
	@ResponseBody
	public BaseResult delCategory(@Validated DelProCategoryParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.delCategory(param, request, response);
	}

	/**
	 * 编辑商品分类排序
	 **/
	@RequestMapping("/v1/editSorts2Category")
	@ResponseBody
	public BaseResult editSorts2Category(@Validated EditSorts2CategoryParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editSorts2Category(param, request, response);
	}

	/**
	 * 获取商品单位列表
	 **/
	@RequestMapping("/v1/getUnits")
	@ResponseBody
	public ListResult<RProUnitBean> getUnits(@Validated GetProUnitsParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProUnitBean> res = new ListResult<RProUnitBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getUnits(param, request, response);
	}

	/**
	 * 新建、编辑商品单位
	 **/
	@RequestMapping("/v1/editUnits")
	@ResponseBody
	public ObjectResult<RProUnitBean> editUnits(@Validated EditProUnitParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProUnitBean> res = new ObjectResult<RProUnitBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editUnits(param, request, response);
	}

	/**
	 * 删除单位信息
	 **/
	@RequestMapping("/v1/delUnit")
	@ResponseBody
	public BaseResult delUnit(@Validated DelProUnitParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.delUnit(param, request, response);
	}

	/**
	 * 启用单位
	 **/
	@RequestMapping("/v1/enableUnit")
	@ResponseBody
	public BaseResult enableUnit(@Validated EnableUnitParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.enableUnit(param, request, response);
	}

	/**
	 * 禁用单位
	 **/
	@RequestMapping("/v1/disableUnit")
	@ResponseBody
	public BaseResult disableUnit(@Validated DisableUnitParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.disableUnit(param, request, response);
	}

	/**
	 * 获取商品列表
	 **/
	@RequestMapping("/v1/getProducts")
	@ResponseBody
	public ListResult<RProductBean> getProducts(@Validated GetProductsParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProductBean> res = new ListResult<RProductBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getProducts(param, request, response);
	}

	/**
	 * 根据id获取商品详情
	 **/
	@RequestMapping("/v1/getDetail")
	@ResponseBody
	public ObjectResult<RProductDetailBean> getDetail(@Validated GetProductDetailParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductDetailBean> res = new ObjectResult<RProductDetailBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getDetail(param, request, response);
	}

	/**
	 * 根据名称或条形码获取商品信息
	 **/
	@RequestMapping("/v1/getProduct")
	@ResponseBody
	public ListResult<RProductBean> getProduct(@Validated GetProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProductBean> res = new ListResult<RProductBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getProduct(param, request, response);
	}

	/**
	 * 新建、编辑商品
	 **/
	@RequestMapping("/v1/editProduct")
	@ResponseBody
	public ObjectResult<RProductDetailBean> editProduct(@Validated EditProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProductDetailBean> res = new ObjectResult<RProductDetailBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editProduct(param, request, response);
	}

	/**
	 * 删除商品信息
	 **/
	@RequestMapping("/v1/delProduct")
	@ResponseBody
	public BaseResult delProduct(@Validated DelProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.delProduct(param, request, response);
	}

	/**
	 * 获取商品价格列表
	 **/
	@RequestMapping("/v1/getPrices")
	@ResponseBody
	public ListResult<RProPriceBean> getPrices(@Validated GetProPricesParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProPriceBean> res = new ListResult<RProPriceBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getPrices(param, request, response);
	}

	/**
	 * 新建、编辑商品价格
	 **/
	@RequestMapping("/v1/editPrice")
	@ResponseBody
	public ObjectResult<RProPriceBean> editPrice(@Validated EditProPriceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProPriceBean> res = new ObjectResult<RProPriceBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editPrice(param, request, response);
	}

	/**
	 * 删除价格信息
	 **/
	@RequestMapping("/v1/delPrice")
	@ResponseBody
	public BaseResult delPrice(@Validated DelProPriceParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.delPrice(param, request, response);
	}

	/**
	 * 启用价格
	 **/
	@RequestMapping("/v1/enablePrice")
	@ResponseBody
	public BaseResult enablePrice(@Validated EnableUnitParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.enablePrice(param, request, response);
	}

	/**
	 * 禁用价格
	 **/
	@RequestMapping("/v1/disablePrice")
	@ResponseBody
	public BaseResult disablePrice(@Validated DisableUnitParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.disablePrice(param, request, response);
	}

	/**
	 * 获取商品售卖状态列表
	 **/
	@RequestMapping("/v1/getSaleStatus")
	@ResponseBody
	public ListResult<RProStatusBean> getSaleStatus(@Validated GetProSaleStatusParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ListResult<RProStatusBean> res = new ListResult<RProStatusBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getSaleStatus(param, request, response);
	}

	/**
	 * 获取商品售卖状态最后失效时间
	 **/
	@RequestMapping("/v1/getLastEndTime")
	@ResponseBody
	public ObjectResult<RProStatusLastTimeBean> getLastEndTime(@Validated GetProStatusLastTimeParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProStatusLastTimeBean> res = new ObjectResult<RProStatusLastTimeBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getLastEndTime(param, request, response);
	}

	/**
	 * 新建、编辑商品状态
	 **/
	@RequestMapping("/v1/editStatus")
	@ResponseBody
	public ObjectResult<RProStatusBean> editStatus(@Validated EditProStatusParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProStatusBean> res = new ObjectResult<RProStatusBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editStatus(param, request, response);
	}

	/**
	 * 删除商品状态信息
	 **/
	@RequestMapping("/v1/delProStatus")
	@ResponseBody
	public BaseResult delProStatus(@Validated DelProStatusParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.delProStatus(param, request, response);
	}

	/**
	 * 商品上架
	 **/
	@RequestMapping("/v1/proSale")
	@ResponseBody
	public BaseResult proSale(@Validated SaleProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.proSale(param, request, response);
	}

	/**
	 * 商品下架
	 **/
	@RequestMapping("/v1/proSoldOut")
	@ResponseBody
	public BaseResult proSoldOut(@Validated SoldOutProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.proSoldOut(param, request, response);
	}

	/**
	 * 获取商品排序列表
	 **/
	@RequestMapping("/v1/getSellList")
	@ResponseBody
	public ObjectResult<RProJSONBean> getSellList(HttpServletRequest request, HttpServletResponse response) {

		return productController.getSellList(request, response);
	}

	/**
	 * 获取商品选择列表
	 **/
	@RequestMapping("/v1/getSelectList")
	@ResponseBody
	public ObjectResult<RProSelectJSONBean> getSelectList(@Validated GetSelectProParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		ObjectResult<RProSelectJSONBean> res = new ObjectResult<RProSelectJSONBean>();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.getSelectList(param, request, response);
	}

	/**
	 * 编辑商品排序
	 **/
	@RequestMapping("/v1/editSorts2Product")
	@ResponseBody
	public BaseResult editSorts2Product(@Validated EditSorts2ProductParam param, BindingResult error, HttpServletRequest request, HttpServletResponse response) {
		BaseResult res = new BaseResult();
		if (error.hasErrors()) {
			res.setRescode(BaseResult.param.getCode());
			res.setMsg(error.getFieldError().getDefaultMessage());
			return res;
		}

		return productController.editSorts2Product(param, request, response);
	}
}
